<?php
namespace api\modules\v1\controllers;

use api\modules\v1\models\circle\Circle;
use api\modules\v1\models\circle\CircleComment;
use api\modules\v1\models\circle\CircleCommentRelation;
use api\modules\v1\models\circle\CircleUserRelation;
use yii\filters\auth\HttpHeaderAuth;
use yii\helpers\ArrayHelper;
use Yii;

/**
 * Default controller for the `v1` module
 */
class CircleController extends DefaultController
{

    public $modelClass = 'api\modules\v1\models\imUser\ImUser';

    public function behaviors()
    {
        return ArrayHelper::merge(parent::behaviors(), [
            'authenticator' => [
                'class' => HttpHeaderAuth::className(),    //head头部验证
                'header' => 'access-token',
                'optional' => [                     //需要过滤掉不用token参数的方法
                    'list','info','comment-list'
                ]
            ]
        ]);
    }

    /**
     * 获取配置
     * @return array
     */
    public function actionConfig() {

        if (Yii::$app->request->isGet) {
            return (new Circle())->getConfig();
        }
    }

    # 发布 | 获取信息
    public function actionInfo($id = 0) {
        $model = new Circle();
        if (\Yii::$app->request->isPost) {
            $model->setScenario('addEdit');
            if ($model->load(\Yii::$app->request->post(), '')) {
                return $model->create();
            }
        }

        if (Yii::$app->request->isGet) {
            return $model->info($id);
        }
    }


    /**
     * 圈子列表
     * @return array
     */
    public function actionList()
    {
        if (Yii::$app->request->isGet) {
            return (new Circle())->getList();
        }
    }

    #评论列表
    public function actionCommentList() {
        $model = new CircleComment();
        if (Yii::$app->request->isGet) {
            return $model->getList();
        }
    }

    /**
     * 删除圈子
     * @return array
     */
    public function actionDelete($id = 0)
    {
        if (Yii::$app->request->isDelete) {
            return (new Circle())->deleteCircle($id);
        }
    }


    /**
     * 圈子点赞
     * @return array
     */
    public function actionLike()
    {
        if (Yii::$app->request->isPost) {
            return (new CircleUserRelation())->like();
        }
    }

    /**
     * 圈子收藏
     * @return array
     */
    public function actionCollect()
    {
        if (Yii::$app->request->isPost) {
            return (new CircleUserRelation())->collection();
        }
    }


    //评论圈子
    public function actionComment() {
        if (Yii::$app->request->isPost) {
            return (new CircleComment())->saveComment();
        }
    }

    //删除评论
    public function actionDeleteComment($id = 0) {
        if (Yii::$app->request->isDelete) {
            return (new CircleComment())->DeleteComment($id);
        }
    }

    /**
     * 评论点赞
     */
    public function actionCommentLike()
    {
        if (Yii::$app->request->isPost) {
            return (new CircleCommentRelation())->like();
        }
    }

    /**
     * 评论和回复我的列表
     */
    public function actionCommentMy()
    {
        if (Yii::$app->request->isGet) {
            return (new CircleComment())->getCommentMyList();
        }
    }

    /**
     * 收到的 点赞  收藏列表
     */
    public function actionRelationList()
    {
        if (Yii::$app->request->isGet) {
            return (new CircleUserRelation())->getRelationList();
        }
    }

}
